package com.medsoft.drpcp;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.Element;

import com.medsoft.drpcp.meta.DrpcpMetaConstant;

public class RouteParser {

	public static void parse(Document drpcpDoc) {
		List<Element> routes = drpcpDoc.getRootElement().element("routetables").elements("routetable");
		for(Element route : routes) {
			RouteItem routeItem = new RouteItem();
			routeItem.setCorpId(StringUtils.isEmpty(route.attributeValue("corpId")) ? "*" : route.attributeValue("corpId"));
			routeItem.setServiceId(StringUtils.isEmpty(route.attributeValue("serviceId")) ? "*" : route.attributeValue("serviceId"));
			routeItem.setNodeName(route.attributeValue("nodeName"));
			routeItem.setSubSystemId(StringUtils.isEmpty(route.attributeValue("subSystemId")) ? "*" : route.attributeValue("subSystemId"));
			routeItem.setSystemId(StringUtils.isEmpty(route.attributeValue("systemId")) ? "*" : route.attributeValue("systemId"));
			routeItem.setVersion(StringUtils.isEmpty(route.attributeValue("version")) ? "*" : route.attributeValue("version"));
			setRouteInfo(routeItem);
		}
	}

	private static void setRouteInfo(RouteItem routeItem) {
		String[] serviceIds = routeItem.getServiceId().split(DrpcpMetaConstant.CONFIG_SEPARATOR);
		String[] subSystemIds = routeItem.getSubSystemId().split(DrpcpMetaConstant.CONFIG_SEPARATOR);
		String[] systemIds = routeItem.getSystemId().split(DrpcpMetaConstant.CONFIG_SEPARATOR);
		String[] versions = routeItem.getVersion().split(DrpcpMetaConstant.CONFIG_SEPARATOR);
		String[] corpIds = routeItem.getCorpId().split(DrpcpMetaConstant.CONFIG_SEPARATOR);
		for(int i=0;i<serviceIds.length;i++) {
			for(int j=0;j<subSystemIds.length;j++) {
				for(int k=0;k<systemIds.length;k++) {
					
					setRouteInfo(serviceIds[i],subSystemIds[j],systemIds[k],routeItem.getNodeName());
					
					for(int m=0;m<versions.length;m++) {
						for(int n=0;n<corpIds.length;n++) {
							RouteItem tmpRouteItem = new RouteItem();
							tmpRouteItem.setCorpId(corpIds[n]);
							tmpRouteItem.setVersion(versions[m]);
							tmpRouteItem.setServiceId(serviceIds[i]);
							tmpRouteItem.setSubSystemId(subSystemIds[j]);
							tmpRouteItem.setSystemId(systemIds[k]);
							tmpRouteItem.setNodeName(routeItem.getNodeName());
							GlobalConfig.routeItems.add(tmpRouteItem);
							if(!(versions[m].equals("*") && corpIds[n].equals("*"))) {
								GlobalConfig.isDynamicRouteEnable = true;
							}
						}
					}
					
				}
			}
		}
	}
	
	private static void setRouteInfo(String serviceId, String subSystemId, String systemId,String nodeName) {
		for (Entry<String, ServiceDefinition> entry :ServiceDefinitionContainer.getAllService().entrySet()) {
			if(entry.getValue().getNodeName() != null) {
				continue;
			}
			
			if (Pattern.matches(serviceId.replace("*", "[0-9]+").replace("?", "[0-9]"), entry.getKey())
					&& Pattern.matches(subSystemId.replace("*", "[0-9]+").replace("?", "[0-9]"), entry.getValue().getSubSystemId())
					&& Pattern.matches(systemId.replace("*", "[0-9]+").replace("?", "[0-9]"), entry.getValue().getSystemId()) ){
				entry.getValue().setNodeName(nodeName);
			}
		}
	}

	public static void main(String[] args) {
		Pattern p = Pattern.compile("12[0-9][0-9]90"); 
        Matcher m = p.matcher("12990"); 
        boolean flg = m.matches(); 
        System.out.println(flg); 
	}
}
